﻿using DryIoc.Microsoft.DependencyInjection;
using MeterReader.WPF.Models;
using MeterReader.WPF.Services;
using MeterReader.WPF.ViewModels;
using MeterReader.WPF.Views;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;

namespace MeterReader.WPF
{
	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : PrismApplication
	{
		protected override Window CreateShell()
		{
			var window = Container.Resolve<MainWindow>();
			return window;
		}

		protected override void RegisterTypes(IContainerRegistry containerRegistry)
		{
			containerRegistry.RegisterForNavigation<MetersDataMonitor>();
			containerRegistry.RegisterForNavigation<MetersCurve>();
			containerRegistry.RegisterForNavigation<History>();
			containerRegistry.RegisterDialog<Message, MessageViewModel>();
		}

		protected override IContainerExtension CreateContainerExtension()
		{
			IServiceCollection services = new ServiceCollection();
			services
				.AddSingleton<IReadData, ReadSimDataService>()
				.AddSingleton<IHistory, HistoryService>()
				.AddSingleton<ISaveData, SaveDataService>()
				.AddDbContext<SimDbContext>(options => options.UseSqlite("DataSource=SimDatas.sqlite"));

			var container = new Container(CreateContainerRules());
			container.WithDependencyInjectionAdapter(services); //DryIoc.Microsoft.DependencyInjection 中方法
			return new DryIocContainerExtension(container);
		}

		protected override Rules CreateContainerRules()
		{
			return Rules
				.Default.WithConcreteTypeDynamicRegistrations(reuse: Reuse.Transient)
				.With(Made.Of(FactoryMethod.ConstructorWithResolvableArguments))
				.WithFuncAndLazyWithoutRegistration()
				.WithTrackingDisposableTransients()
				//.WithoutFastExpressionCompiler() // Prism 版本原因已经删除了它，不重写会报错
				.WithFactorySelector(Rules.SelectLastRegisteredFactory());
		}
	}
}